package com.test.mvc.controller;

import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.springframework.web.servlet.support.RequestContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * Created by xiaomeng.chen on 2015/12/15.
 */
@Controller
public class Hello {
    @RequestMapping("/test")
    public String index(HttpServletRequest request, Model model) {
//        System.out.println(request.getSession().getAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME));
        RequestContext requestContext = new RequestContext(request);
//        System.out.println(requestContext.getMessage("header.message"));
        model.addAttribute("title", requestContext.getMessage("header.title"));
        model.addAttribute("message", requestContext.getMessage("header.message"));
        model.addAttribute("zh_CN", requestContext.getMessage("header.zh_CN"));
        model.addAttribute("en_US", requestContext.getMessage("header.en_US"));
        return "i18n";
    }

//    @RequestMapping("/test1")
//    public String test1(HttpServletRequest request, Model model) {
////        System.out.println(request.getSession().getAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME));
//        RequestContext requestContext = new RequestContext(request);
////        System.out.println(requestContext.getMessage("header.message"));
//        model.addAttribute("title", requestContext.getMessage("header.title"));
//        model.addAttribute("message", requestContext.getMessage("header.message"));
//        model.addAttribute("zh_CN", requestContext.getMessage("header.zh_CN"));
//        model.addAttribute("en_US", requestContext.getMessage("header.en_US"));
//        return "i18n";
//    }

    @RequestMapping("/lang")
    @ResponseBody
    public String lang(HttpServletRequest request) {
        String langType = request.getParameter("langType");
        if (StringUtils.isEmpty(langType))
            request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, LocaleContextHolder.getLocale());
        else {
            if (langType.equals("zh")) {
                Locale locale = new Locale("zh", "CN");
                request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, locale);
            } else if (langType.equals("en")) {
                Locale locale = new Locale("en", "US");
                request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, locale);
            } else
                request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, LocaleContextHolder.getLocale());
        }
        return null;
    }

    @RequestMapping("/lang1")
    @ResponseBody
    public String lang(HttpServletRequest request, HttpServletResponse response) {
        String langType = request.getParameter("langType");
        if (StringUtils.isEmpty(langType))
            new CookieLocaleResolver().setLocale(request, response, LocaleContextHolder.getLocale());
        else {
            if (langType.equals("zh")) {
                Locale locale = new Locale("zh", "CN");
                new CookieLocaleResolver().setLocale(request, response, locale);
            } else if (langType.equals("en")) {
                Locale locale = new Locale("en", "US");
                new CookieLocaleResolver().setLocale(request, response, locale);
            } else
                new CookieLocaleResolver().setLocale(request, response, LocaleContextHolder.getLocale());
        }
        return null;
    }
}
